/**
 * @Author: songgh
 * @Date: 2021-09-20 14:41:08
 * @LastEditors: songgh
 * @LastEditTime: 2021-09-20 15:41:08
 * @Description: empty
 */
import { defineComponent, onMounted, reactive } from "vue";
import { loginByToken } from "@src/api";
import { useRouter } from "vue-router";
import { useStore } from "vuex";

interface IState {
    flag: boolean;
}

export default defineComponent({
    name: "Empty",
    setup() {
        const state = reactive<IState>({
            flag: false,
        });
        const store = useStore();
        const router = useRouter();
        const accessToken = router.currentRoute.value.query?.accessToken as string;

        onMounted(async () => {
            if (accessToken) {
                try {
                    const data = await loginByToken({ token: accessToken });
                    store.dispatch("user/setUserInfo", data);
                    router.push("/home");
                } catch (error) {
                    state.flag = true;
                    console.error(error);
                }
            } else {
                console.error(`haven't accessToken`);
                state.flag = true;
            }
        });

        return {
            state,
        };
    },
    render() {
        return (
            <div class="h-screen flex justify-center items-center">
                {this.state.flag ? <van-empty
                    image="error"
                    description="页面走丢了"
                /> : <div></div>}
            </div>
        );
    },
});
